package com.ruoyi.web.controller.market.account;

import java.util.List;
import java.util.Map;

import com.ruoyi.account.service.IAccountInfoService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.db.domain.AccountInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.db.domain.AccountInfoHis;
import com.ruoyi.account.service.IAccountInfoHisService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.wapper.AccountInfoHisWapper;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 总账户流水Controller
 * 
 * @author ruoyi
 * @date 2023-02-02
 */
@Controller
@RequestMapping("/account/infoHis")
public class AccountInfoHisController extends BaseController
{
    private String prefix = "account/infoHis";

    @Autowired
    private IAccountInfoHisService accountInfoHisService;

    @Autowired
    private IAccountInfoService accountInfoService;

    @RequiresPermissions("account:infoHis:view")
    @GetMapping("/{accountId}")
    public String infoHis(@PathVariable("accountId") String accountId, ModelMap mmap)
    {
        mmap.put("accountId", accountId);
        return prefix + "/infoHis";
    }

    /**
     * 查询总账户流水列表
     */
    @RequiresPermissions("account:infoHis:view")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AccountInfoHis accountInfoHis)
    {
        startPage();
        List<Map<String, Object>> mapList = accountInfoHisService.selectAccountInfoHisListMap(accountInfoHis);
        mapList = new AccountInfoHisWapper(mapList).wrap();
        return getDataTable(mapList);
    }

    /**
     * 导出总账户流水列表
     */
    @RequiresPermissions("account:infoHis:export")
    @Log(title = "总账户流水", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AccountInfoHis accountInfoHis)
    {
        List<AccountInfoHis> list = accountInfoHisService.selectAccountInfoHisList(accountInfoHis);
        ExcelUtil<AccountInfoHis> util = new ExcelUtil<AccountInfoHis>(AccountInfoHis.class);
        return util.exportExcel(list, "总账户流水数据");
    }

    /**
     * 新增总账户流水
     */
    @GetMapping("/add/{accountId}")
    public String add(@PathVariable("accountId") String accountId, ModelMap mmap)
    {
        mmap.put("accountId", accountId);
        return prefix + "/add";
    }

    /**
     * 新增保存总账户流水
     */
    @RequiresPermissions("account:infoHis:add")
    @Log(title = "总账户流水", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AccountInfoHis accountInfoHis)
    {
        accountInfoHis.setCreateTime(DateUtils.getNowDate());
        SysUser sysUser = getSysUser();
        accountInfoHis.setOperatorId(sysUser.getUserId().toString());
        accountInfoHis.setOperatorName(sysUser.getUserName());
        accountInfoHis.setStatus(1L);
        AccountInfo accountInfo = accountInfoService.selectAccountInfoById(accountInfoHis.getAccountId());
        accountInfoHis.setAccountType(accountInfo.getType());
        accountInfoHis.setAccountName(accountInfo.getName());
        accountInfoHis.setAccountUser(accountInfo.getAccountUser());
        accountInfoHis.setAccountNo(accountInfo.getAccountNo());
        accountInfoHis.setOriginAmount(accountInfo.getTotalAmount());
        accountInfoHis.setCurrentAmount(accountInfoHis.getOriginAmount().add(accountInfoHis.getChangeAmount()));

        return toAjax(accountInfoHisService.insertAccountInfoHis(accountInfoHis));
    }

    /**
     * 修改总账户流水
     */
    @RequiresPermissions("account:infoHis:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        AccountInfoHis accountInfoHis = accountInfoHisService.selectAccountInfoHisById(id);
        mmap.put("accountInfoHis", accountInfoHis);
        return prefix + "/edit";
    }

    /**
     * 修改保存总账户流水
     */
    @RequiresPermissions("account:infoHis:edit")
    @Log(title = "总账户流水", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AccountInfoHis accountInfoHis)
    {
        return toAjax(accountInfoHisService.updateAccountInfoHis(accountInfoHis));
    }

    /**
     * 删除总账户流水
     */
    @RequiresPermissions("account:infoHis:remove")
    @Log(title = "总账户流水", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(accountInfoHisService.deleteAccountInfoHisByIds(ids));
    }
}
